package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.GroupHorizontalAdapter;
import com.hxkcloud.nurse.adapter.GroupSelectorPopwindowAdapter;
import com.hxkcloud.nurse.beans.DoctorGroupBean;
import com.hxkcloud.nurse.utils.BrightnessUtils;

import java.util.List;

/**
 * Created by william_sun on 2016-09-25.
 * Description:分组纵向选择
 */
//public class GroupSelectorPopWindow extends PopupWindow implements View.OnClickListener, OnWheelScrollListener {
public class GroupSelectorPopWindow extends PopupWindow implements View.OnClickListener{
    private Activity context;
   // private WheelView wheel_groups;
    private ListView listView;
    private TextView bt_cancel;
  //  private Button bt_sure;

    private List<DoctorGroupBean> group_lists;
    private String groups[];
    private int item = 0;
    private GroupHorizontalAdapter.onGroupHorizontalClickListener listener;
    private String curIds;

    public GroupSelectorPopWindow(Activity context, GroupHorizontalAdapter.onGroupHorizontalClickListener listener) {
        this.context = context;
        this.listener = listener;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectgroup, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
      //  wheel_groups = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (TextView) rootView.findViewById(R.id.bt_selectaddress_cancel);
        listView = (ListView) rootView.findViewById(R.id.popup_select_listview);
       // bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);

    }

    private void initDatas() {

//        wheel_groups.setCyclic(false);
//
//        wheel_groups.setVisibleItems(4);

    }

    private void initListener() {
       // bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent, final List<DoctorGroupBean> group_list, final String curId) {
        group_lists = group_list;
        curIds = curId;
        groups = new String[group_list.size()];
        int curItem = 0;
        for (int i = 0; i < group_list.size(); i++) {
            groups[i] = group_list.get(i).getName();
            if (!TextUtils.isEmpty(curId)) {
                if (group_list.get(i).getGroupId().equals(curId)) {
                    curItem = i;
                }
            }
        }
        final GroupSelectorPopwindowAdapter adapters = new GroupSelectorPopwindowAdapter(context,groups);
        listView.setAdapter(adapters);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < groups.length; i++) {
                    if (!TextUtils.isEmpty(curIds)) {
                        if (group_list.get(i).getGroupId().equals(curIds)) {
                            adapters.changeSelected(position);
                            item = position;
                        }
                    }
                }

                if (listener != null){
                    listener.onClick(item);
                }

            }
        });
 //       ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, groups);
//        wheel_groups.setViewAdapter(adapter);
//        wheel_groups.setCurrentItem(curItem);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);

    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
//            case R.id.bt_selectaddress_sure:
//                if (listener != null)
//                   // listener.onClick(wheel_groups.getCurrentItem());
//                listener.onClick(item);
//
//                break;
            case R.id.bt_selectaddress_cancel:

                break;
        }
    }

//    @Override
//    public void onScrollingStarted(WheelView wheel) {
//
//    }
//
//    @Override
//    public void onScrollingFinished(WheelView wheel) {
//
//    }

}
